import { _decorator, Color, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { Hero, NumberStrList } from '../entity/Hero';
import { EventManager } from '../event/EventManager';
import { UIManager } from '../common/UIManager';
import HeroApi from '../api/HeroApi';
import { PrefabEnum } from '../common/PrefabEnum';
const { ccclass, property } = _decorator;

@ccclass('SelectHeroItem')
export class SelectHeroItem extends Component {

    @property(Sprite)
    cover:Sprite
    @property(Label)
    heroName:Label
    @property(Label)
    level:Label
    @property(Node)
    select:Node
    @property(Label)
    selectLabel:Label
    @property(SpriteFrame)
    btn1:SpriteFrame
    @property(SpriteFrame)
    btn2:SpriteFrame

    private hero:Hero
    private loc:number
    start() {
        this.select.on(Node.EventType.TOUCH_END, this.onSelect, this)
    }

    async setData(hero:Hero,loc:number){
        this.hero = hero
        this.loc = loc
        this.cover.spriteFrame = await hero.getHeadImg()
        this.heroName.string = hero.name
        this.level.string = `${NumberStrList[hero.ad]}阶·${hero.en}星·${hero.l}级`
        
        if(hero.loc == 0){
           
        } else {
            this.select.getComponent(Sprite).spriteFrame = this.btn2
            this.selectLabel.string = '已在队列中'
           
        }
    }

    async onSelect(){
        if(this.hero.loc != 0){
            return
        }

        await UIManager.instance.showLoading()
        let res = await HeroApi.Execute({
            op: 7,
            hid: this.hero.id,
            seat: this.loc
        })
        UIManager.instance.closeLoading()

        EventManager.instance.emit(EventManager.EventType.HeroLocationChange, 1)
        UIManager.instance.closeUI(PrefabEnum.SelectHero)
    }
}


